package com.pas.book;

public class ColorUtil {

    public static int gray(int percent) {
        if (percent < 0 || percent > 100) throw new IllegalArgumentException();
        int value = 0xff / 100 * percent;
        return rgb(value, value, value);
    }

    public static int gray(double n) {
        if (n < 0 || n > 1) throw new IllegalArgumentException();
        int value = (int) (0xff * n);
        return rgb(value, value, value);
    }

    public static int brighter(int color, int delta) {
        int r = ((color >> 16) & 0xff) + delta;
        int g = ((color >> 8) & 0xff) + delta;
        int b = (color & 0xff) + delta;
        return rgb(r, g, b);
    }

    public static int darker(int color, int delta) {
        return brighter(color, -delta);
    }

    public static int rgb(int r, int g, int b) {
        return normalize(b) | (normalize(g) << 8) | (normalize(r) << 16);
    }

    private static int normalize(int value) {
        if (value < 0) return 0;
        if (value > 0xff) return 0xff;
        return value;
    }
}
